#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "timers.h"

double calcEX(double x);
double calcField(double start, double stop, double h);
long int recTimer();

main(int argc, char **argv)
{
	long int time_start = recTimer();
	int n = 1024;

	if(argc>1) {
		n = (int) strtol(argv[1], NULL, 10);

	}
	int a,b;
	a = -2;
	b = 2;
	

	double totalField = 0;

	int i,j;
	double start_a, stop_b;
	double jump = 4;
	jump = jump/n;
	double offset = n;

	for(i=0; i<offset; i++) {
		start_a = -2 + (jump * i);
		stop_b = start_a + jump;
		totalField = totalField + calcField(start_a, stop_b, jump);

	}

	printf("%0.10f \n", totalField);

	printf("Czas całego programu: %f \n", (double)(recTimer()-time_start) / 1000000000);

}

double calcEX(double x) {

	return pow(M_E, -pow(x, 2));

}

double calcField(double start, double stop, double h) {

	return ((calcEX(start) + calcEX(stop)) * h) / 2;

}
long int recTimer() {
	struct timespec t;
	clock_gettime(CLOCK_MONOTONIC, &t);
	return ((t.tv_sec * TIMER_SCALE) + t.tv_nsec);
}